/*
**************************************************************************************
*Myriad Engine                                                                       *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
package leola.backend.interpreter;

import leola.backend.EvalException;
import leola.backend.interpreter.LeoTypeConverter.JavaTypeVisitor;
import leola.types.LeoNull;
import leola.types.LeoNumber;
import leola.types.LeoObject;
import leola.types.LeoObject.LeoType;

/**
 * @author Tony
 *
 */
public class ObjectArrayJavaTypeVisitor implements JavaTypeVisitor {

	public Object params[];
	public int currentIndex;
	
	public void onBoolean(LeoObject obj) {
		this.params[this.currentIndex] = LeoObject.isTrue(obj);
	}

	public void onByte(LeoObject obj) throws Exception {
		if ( obj != null && obj.isOfType(LeoType.NUMBER)) {
			LeoNumber n = obj.as();
			this.params[this.currentIndex] = (byte)n.getNumber();	
		}
		else {
			throw new EvalException
				("The supplied LeoObject: " + obj + " does not match the native type: byte");
		}
	}

	public void onDouble(LeoObject obj) throws Exception {
		if ( obj != null && obj.isOfType(LeoType.NUMBER)) {
			LeoNumber n = obj.as();
			this.params[this.currentIndex] = n.getNumber();	
		}
		else {
			throw new EvalException
				("The supplied LeoObject: " + obj + " does not match the native type: " + this.params[this.currentIndex]);
		}
	}

	public void onFloat(LeoObject obj) throws Exception {
		if ( obj != null && obj.isOfType(LeoType.NUMBER)) {
			LeoNumber n = obj.as();
			this.params[this.currentIndex] = (float)n.getNumber();	
		}
		else {
			throw new EvalException
				("The supplied LeoObject: " + obj + " does not match the native type: " + this.params[this.currentIndex]);
		}
	}

	public void onInt(LeoObject obj) throws Exception {
		if ( obj != null && obj.isOfType(LeoType.NUMBER)) {
			LeoNumber n = obj.as();
			this.params[this.currentIndex] = (int)n.getNumber();	
		}
		else {
			throw new EvalException
				("The supplied LeoObject: " + obj + " does not match the native type: int");
		}
	}

	public void onLong(LeoObject obj) throws Exception {
		if ( obj != null && obj.isOfType(LeoType.NUMBER)) {
			LeoNumber n = obj.as();
			this.params[this.currentIndex] = (long)n.getNumber();	
		}
		else {
			throw new EvalException
				("The supplied LeoObject: " + obj + " does not match the native type: " + this.params[this.currentIndex]);
		}
	}

	public void onObject(LeoObject obj) throws Exception {
		if ( obj == null || obj.isOfType(LeoType.NULL)) {
			this.params[this.currentIndex] = LeoNull.LEONULL;
		}
		else {
			this.params[this.currentIndex] = obj.getValue();
		}
	}

	public void onShort(LeoObject obj) throws Exception {
		if ( obj != null && obj.isOfType(LeoType.NUMBER)) {
			LeoNumber n = obj.as();
			this.params[this.currentIndex] = (short)n.getNumber();	
		}
		else {
			throw new EvalException
				("The supplied LeoObject: " + obj + " does not match the native type: " + this.params[this.currentIndex]);
		}
	}

	public void onString(LeoObject obj) throws Exception {
		if ( obj == null || obj.isOfType(LeoType.NULL)) {
			this.params[this.currentIndex] = LeoNull.LEONULL;
		} 
		else {		
			this.params[this.currentIndex] = obj.toString();	
		}
	}

	/* (non-Javadoc)
	 * @see leola.backend.interpreter.LeoTypeConverter.JavaTypeVisitor#onLeoObject(leola.types.LeoObject)
	 */
	@Override
	public void onLeoObject(LeoObject obj) throws Exception {
		if ( obj == null || obj.isOfType(LeoType.NULL)) {
			this.params[this.currentIndex] = LeoNull.LEONULL;
		} 
		else {		
			this.params[this.currentIndex] = obj;	
		}		
	}	
}
